package com.ad.pet.app.photo.loader.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.photo.loader.ImageAdapter;
import com.ad.pet.app.photo.model.ImageItem;
import com.ad.pet.app.photo.model.PhotoItem;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**1000113200029318608
 * Created by hcqi on.
 * Des:图片fragment
 * Date: 2017/3/31
 */

public class ImageFragment extends BaseFragment {
    private List<ImageItem> mDatas;
    private ImageAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private PhotoLoaderActivity mActivity;
    private ToastUtil mToastUtil;
    private LinearLayout mLayoutBottomBar;
    private TextView mTvPreview;
    private CheckBox mCbOrigin;
    private Button mButSend;

    @Override
    public int initContentView() {
        return R.layout.fragment_photo_image;
    }


    @Override
    protected void initData() {
        mToastUtil = new ToastUtil(getActivity());
        mActivity = (PhotoLoaderActivity) getActivity();
        int selectedMax = mActivity.getSelectedMax();
        mAdapter = new ImageAdapter(mDatas, selectedMax);
        if (selectedMax > 1) {
            mLayoutBottomBar.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initEvents() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.HORIZONTAL));
        mRecyclerView.setAdapter(mAdapter);

        //点击事件
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                //如果是0 直接返回
                if (mActivity.getSelectedMax() == 0) {
                    Intent intent =
                            getActivity().getIntent();
                    ArrayList<ImageItem> imageItems = new ArrayList<>();
                    imageItems.add(mDatas.get(position));
                    intent.putExtra("path", imageItems);
                    getActivity().setResult(Activity.RESULT_OK, intent);
                    getActivity().finish();
                } else {
                    selected(position);
//                    Navigation.goPhotoMaxListSelectActivity(mFragment, (ArrayList<ImageItem>) mAdapter.getData(), position, mActivity.getSelectedMax());
                }
            }
        });
        //子控件点击事件
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {

            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                selected(position);
            }
        });

        //预览
        mTvPreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdapter.getSelectCount() < 1) {
                    mToastUtil.showShort(getString(R.string.please_chose_photo));
                    return;
                }
                ArrayList<PhotoItem> photoItems = new ArrayList<>();
                ArrayList<ImageItem> selectedData = mAdapter.getSelectedData();
                for (int i = 0; i < selectedData.size(); i++) {
                    ImageItem imageItem = selectedData.get(i);
                    photoItems.add(new PhotoItem(imageItem.getImagePath(), imageItem.getImagePath()));
                }
                Navigation.goPhotoMaxListActivity(mActivity, photoItems, 0);
            }
        });
        //发送·按钮
        mButSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdapter.getSelectCount() < 1) {
                    mToastUtil.showShort(getString(R.string.please_chose_photo));
                    return;
                }
                ArrayList<ImageItem> selectedData = mAdapter.getSelectedData();
//                boolean checked = mCbOrigin.isChecked();
                for (ImageItem imageItem : selectedData) {
                    imageItem.setOriginal(true);
                }
                send(selectedData);
            }
        });
    }

    private void selected(int position) {
        ImageItem imageItem = mAdapter.getData().get(position);
        boolean selected = imageItem.isSelected();
        if (!selected && getSelectedData().size() == mActivity.getSelectedMax()) {
            mToastUtil.showShort("MOST:" + mActivity.getSelectedMax());
            return;
        }
        imageItem.setSelected(!selected);
        if (!imageItem.isSelected())
            mAdapter.setSelectCount(false);
        else mAdapter.setSelectCount(true);
        mButSend.setText(String.format("Sure(%d)", mAdapter.getSelectCount()));
        mAdapter.getData().set(position, imageItem);
        mAdapter.notifyItemChanged(position);
    }

    private void send(ArrayList<ImageItem> selectedData) {
        Intent intent = getActivity().getIntent();
        intent.putParcelableArrayListExtra("path", selectedData);
        getActivity().setResult(RESULT_OK, intent);
        getActivity().finish();
    }

    @Override
    protected void initViews() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mLayoutBottomBar = findViewById(R.id.layout_bottom_bar);
        mTvPreview = findViewById(R.id.tv_preview);
        mCbOrigin = findViewById(R.id.cb_origin);
        mButSend = findViewById(R.id.but_send);
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdapter != null) {
            int selectCount = mAdapter.getSelectedData().size();
            mButSend.setText(String.format("send(%d)", selectCount));
            mAdapter.setSelectCount(selectCount);
        }
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    public static ImageFragment newInstance(List<ImageItem> imageItems) {
        Bundle args = new Bundle();
        ImageFragment fragment = new ImageFragment();
        fragment.mDatas = imageItems;
        fragment.setArguments(args);
        return fragment;
    }

    public ArrayList<ImageItem> getSelectedData() {
        return mAdapter.getSelectedData();
    }

    public void resetSelectData() {
        mAdapter.resetSelectData();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            //是发送返回,还是按返回键返回
            int send = data.getIntExtra("send", 0);
            ArrayList<ImageItem> imageItems = data.getParcelableArrayListExtra("path");
            mAdapter.setNewData(imageItems);
            if (send == 1) {
                //发送方法
                send(mAdapter.getSelectedData());
            }
        }
    }
}
